package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import controller.MainController;

public class MainView {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5739927886065724973L;

	private JTextArea contenu;
	private JFrame mainFrame;
	private JTextField commande;

	private MainController mainController;

	int index;

	public MainView() {
		/*
		 * Initialisation du controller de commandes
		 */
		mainController = new MainController(this);

		/*
		 * Initialisation des composants de la vue
		 */
		mainFrame = new JFrame("Gestion des consultants");
		mainFrame.setLayout(new BorderLayout());

		contenu = new JTextArea();
		contenu.setEnabled(true);
		contenu.setEditable(false);

		commande = new JTextField();

		index = 0;

		JPanel btnPanel = new JPanel(new FlowLayout());
		btnPanel.add(new JButton(new AbstractAction("<") {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				List<String> cmd = mainController.getCommandesHistory();

				if (cmd.size() == 0) {
					return;
				}

				index = index + 1 >= cmd.size() ? index : index + 1;
				commande.setText(cmd.get(index));

			}
		}));
		btnPanel.add(new JButton(new AbstractAction(">") {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				List<String> cmd = mainController.getCommandesHistory();

				if (cmd.size() == 0) {
					return;
				}

				index = index <= 0 ? 0 : index - 1;
				commande.setText(cmd.get(index));
			}
		}));
		JPanel execPanel = new JPanel(new FlowLayout());
		execPanel.add(new JButton(new AbstractAction("Valider") {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainController.commande(commande.getText());
				commande.setText("");
				index = 0;
			}
		}));

		JPanel panel = new JPanel(new BorderLayout());
		panel.add(btnPanel, BorderLayout.LINE_START);
		panel.add(commande, BorderLayout.CENTER);
		panel.add(execPanel, BorderLayout.LINE_END);

		// Initialisation des evenements du textfield
		commande.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent evt) {

				switch (evt.getKeyChar()) {
				case '\n':

					mainController.commande(commande.getText());
					commande.setText("");
					index = 0;
					break;
				}

			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyPressed(KeyEvent evt) {

			}
		});

		/*
		 * Ajout des composants a la vue
		 */
		mainFrame.add(new JScrollPane(contenu), BorderLayout.CENTER);
		mainFrame.add(panel, BorderLayout.PAGE_END);
		mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		mainFrame.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				mainController.exit();
			}
		});

		// Initilisation des attributs de la vue

		mainFrame.pack();
		mainFrame.setSize(800, 400);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension size = mainFrame.getSize();
		mainFrame.setLocation(screenSize.width / 2 - size.width / 2,
				screenSize.height / 2 - size.height / 2);

		mainFrame.setVisible(true);
	}

	/**
	 * Affiche le contenu sur l'ecran principal
	 * 
	 * @param contenu
	 */
	public void afficher(String message) {
		// "\n" permet un retour a la ligne
		contenu.append(message + "\n");
		contenu.repaint();
	}

	/**
	 * Efface tout ce qui est affiché sur l'ecran principal
	 */
	public void effacer() {
		contenu.setText("");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e1) {
		}

		MainView frame = new MainView();
	}
}
